import { HTTP } from "../utils/http.js";

export class Index extends HTTP{
    login(code) {
      return this.request({
        url: 'wxapp/openid',
        data: {
          code
        }
      })
    }
    
    getHome(read_id = '', hash = '', params={}) {
        return this.request({
            url: 'wxapp/index',
            data: { read_id, hash, ...params }
        })
    }

    getSwitch(id) {
        return this.request({
          url: 'wxapp/switch',
          method: 'POST',
          data: { id }
        })
    }

    getStart() {
      return new Promise((resolve, reject)=>{
        let startData = wx.getStorageSync('startData')
        let timestamp = Date.parse(new Date())
        if (startData && startData.expire > timestamp) {
          resolve(startData.data)
        } else {
          this.request({ url: 'wxapp/start' }).then(data=>{
            resolve(data)
            wx.setStorageSync('startData', { data, expire:timestamp + 600 * 1000 })
          })
        }
      })
    }

    getTask() {
      return this.request({
        url: 'activity/task'
      })
    }

    getActivity() {
      return this.request({
        url: 'activity/activity'
      })
    }

    getUserIndex() {
      return this.request({
        url: 'user/index'
      })
    }

    getMallIndex(page, version='new') {
      return this.request({
        url: 'mall/index',
        data: {
          page,
          version
        }
      })
    }

    getIntegralDetail() {
      return this.request({
        url: 'user/credit_detail',
        method: 'POST'
      })
    }

    postFeedback(content,contact) {
      return this.request({
        url: 'user/feedback',
        method: 'POST',
        data: {
          content, contact
        }
      })
    }
}